
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: '',
    apiUrl: '',
    date: "请选择时间",
    result: {
      "morning": [
        {
          "timer": "06:00-07:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "07:00-08:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "08:00-09:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "09:00-10:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "10:00-11:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "11:00-12:00",
          "num": 4,
          "isSelect": false
        },
      ],
      "afternoon": [
        {
          "timer": "12:00-13:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "13:00-14:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "14:00-15:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "15:00-16:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "16:00-17:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "17:00-18:00",
          "num": 4,
          "isSelect": false
        },
      ],
      "night": [
        {
          "timer": "18:00-19:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "19:00-20:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "10:00-21:00",
          "num": 4,
          "isSelect": false
        },
        {
          "timer": "21:00-22:00",
          "num": 4,
          "isSelect": false
        },
      ]
    }
  },
  //提交
  submitTorelse: function () {
    if (this.data.date == "请选择时间") {
      wx.showToast({
        title: '未选择时间',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    var params = {};
    var userInfo = wx.getStorageSync("userInfo");
    params.session_key = wx.getStorageSync("session_key");
    params.openid = wx.getStorageSync("openid");
    params.cid = userInfo.cid;
    params.date = this.data.date;
    params.status = userInfo.status;

    params.results = [];
    var result = this.data.result;

    var result_morning = result.morning;
    result_morning.forEach((item) => {
      if (item.isSelect) {
        var morningItem = {};
        morningItem.cid = userInfo.cid;
        morningItem.cname = userInfo.name != null ? userInfo.name : userInfo.nickname;
        morningItem.period = item.timer;
        morningItem.time = 0;
        morningItem.date = this.data.date;
        morningItem.number = item.num;
        morningItem.people_num = 0;
        morningItem.subject = userInfo.subject;
        morningItem.vehicle_type = userInfo.vehicleType;
        morningItem.status = userInfo.status;
        params.results.push(morningItem);
      }
    })

    var result_afternoon = result.afternoon;
    result_afternoon.forEach((item) => {
      if (item.isSelect) {
        var morningItem = {};
        morningItem.cid = userInfo.cid;
        morningItem.cname = userInfo.name != null ? userInfo.name : userInfo.nickname;
        morningItem.period = item.timer;
        morningItem.time = 1;
        morningItem.date = this.data.date;
        morningItem.number = item.num;
        morningItem.people_num = 0;
        morningItem.subject = userInfo.subject;
        morningItem.vehicle_type = userInfo.vehicleType;
        morningItem.status = userInfo.status;
        params.results.push(morningItem);
      }
    })

    var result_night = result.night;
    result_night.forEach((item) => {
      if (item.isSelect) {
        var morningItem = {};
        morningItem.cid = userInfo.cid;
        morningItem.cname = userInfo.name != null ? userInfo.name : userInfo.nickname;
        morningItem.period = item.timer;
        morningItem.time = 2;
        morningItem.date = this.data.date;
        morningItem.number = item.num;
        morningItem.people_num = 0;
        morningItem.subject = userInfo.subject;
        morningItem.vehicle_type = userInfo.vehicleType;
        morningItem.status = userInfo.status;
        params.results.push(morningItem);
      }
    })

    console.log(params);
    // 发起网络请求
    wx.request({
      url: this.data.apiUrl + '/rele/replan.do',
      data: {
        params: JSON.stringify(params)
      },
      method: 'POST',
      success: (res) => {
        console.log(res, '重新发布成功返回');
        wx.showToast({
          title: '重新发布成功',
          icon: 'success',
          duration: 1500
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      staticUrl: app.globalData.staticUrl,
      apiUrl: app.globalData.apiUrl,
      date: options.date
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  move(e) {
    // console.log(e.currentTarget.dataset.index);
    if (e.currentTarget.dataset.class == 1) {
      if (!this.data.result.morning[e.currentTarget.dataset.index].isSelect) {
        return false;
      }
      var index = 'result.morning[' + e.currentTarget.dataset.index + '].num';
      this.setData({
        [index]: this.data.result.morning[e.currentTarget.dataset.index].num - 1
      })
    } else if (e.currentTarget.dataset.class == 2) {
      if (!this.data.result.afternoon[e.currentTarget.dataset.index].isSelect) {
        return false;
      }
      var index = 'result.afternoon[' + e.currentTarget.dataset.index + '].num';
      this.setData({
        [index]: this.data.result.afternoon[e.currentTarget.dataset.index].num - 1
      })
    } else if (e.currentTarget.dataset.class == 3) {
      if (!this.data.result.night[e.currentTarget.dataset.index].isSelect) {
        return false;
      }
      var index = 'result.night[' + e.currentTarget.dataset.index + '].num';
      this.setData({
        [index]: this.data.result.night[e.currentTarget.dataset.index].num - 1
      })
    }
  },
  selectItem(e) {
    if (e.currentTarget.dataset.class == 1) {
      var index = 'result.morning[' + e.currentTarget.dataset.index + '].isSelect';
      this.setData({
        [index]: !this.data.result.morning[e.currentTarget.dataset.index].isSelect
      })
    } else if (e.currentTarget.dataset.class == 2) {
      var index = 'result.afternoon[' + e.currentTarget.dataset.index + '].isSelect';
      this.setData({
        [index]: !this.data.result.afternoon[e.currentTarget.dataset.index].isSelect
      })
    } else if (e.currentTarget.dataset.class == 3) {
      var index = 'result.night[' + e.currentTarget.dataset.index + '].isSelect';
      this.setData({
        [index]: !this.data.result.night[e.currentTarget.dataset.index].isSelect
      })
    }
  },
  add(e) {
    // console.log(e.currentTarget.dataset.index);
    if (e.currentTarget.dataset.class == 1) {
      if (!this.data.result.morning[e.currentTarget.dataset.index].isSelect) {
        return false;
      }
      var index = 'result.morning[' + e.currentTarget.dataset.index + '].num';
      this.setData({
        [index]: this.data.result.morning[e.currentTarget.dataset.index].num + 1
      })
    } else if (e.currentTarget.dataset.class == 2) {
      if (!this.data.result.afternoon[e.currentTarget.dataset.index].isSelect) {
        return false;
      }
      var index = 'result.afternoon[' + e.currentTarget.dataset.index + '].num';
      this.setData({
        [index]: this.data.result.afternoon[e.currentTarget.dataset.index].num + 1
      })
    } else if (e.currentTarget.dataset.class == 3) {
      if (!this.data.result.night[e.currentTarget.dataset.index].isSelect) {
        return false;
      }
      var index = 'result.night[' + e.currentTarget.dataset.index + '].num';
      this.setData({
        [index]: this.data.result.night[e.currentTarget.dataset.index].num + 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})